package com.jd.service.impl;

import com.alibaba.fastjson.JSON;
import com.jd.common.Page;
import com.jd.dao.BrowseMapper;
import com.jd.dao.RectabMapper;
import com.jd.dao.ReviewMapper;
import com.jd.dao.UserMapper;
import com.jd.pojo.BrowseExample;
import com.jd.pojo.RectabExample;
import com.jd.pojo.ReviewExample;
import com.jd.pojo.User;
import com.jd.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private BrowseMapper browseMapper;

    @Autowired
    private RectabMapper rectabMapper;

    @Override
    public Page<User> selectUserList(Integer page, Integer rows, String username) {

        User user = new User();
        if (StringUtils.isNotBlank(username)) {
            user.setUsername(username);
        }
        // 当前页的用户数
        user.setStart((page-1)*rows);
        // 每页数
        user.setRows(rows);
        System.out.println("user===" + JSON.toJSONString(user));
        //分页查询user列表
        List<User> userList = userMapper.selectUserList(user);
        //总记录数
        Integer count = userMapper.selectUserCount(user);
        Page<User> result = new Page<>();
        result.setPage(page);
        result.setRows(userList);
        result.setSize(rows);
        result.setTotal(count);
        System.out.println("result===" + JSON.toJSONString(result));
        return result;

    }

    @Override
    public void deleteUser(Integer id) {

        //删除用户对电影的评分 review表
        ReviewExample example = new ReviewExample();
        ReviewExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(id);
        int i = reviewMapper.deleteByExample(example);


        //删除用户收藏的电影 browse表
        BrowseExample example1 = new BrowseExample();
        BrowseExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUseridEqualTo(id);
        int i1 = browseMapper.deleteByExample(example1);

        //删除向用户推荐的电影列表   rectab表
        RectabExample example2 = new RectabExample();
        RectabExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andUseridEqualTo(id);
        int i2 = rectabMapper.deleteByExample(example2);

        //最后删除用户表中的信息
        int i3 = userMapper.deleteByPrimaryKey(id);

        System.out.println("i = " + i + ", i1 = " + i1 + ", i2 = " + i2 + ", i3 = " + i3);
    }

    /**
     * 修改用户信息时,用户信息回显
     * @param id
     * @return
     */
    @Override
    public User getUserInfo(Integer id) {

        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    /**
     * 修改用户信息
     * @param user
     */
    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }


    /**
     * 添加用户
     * @param user
     */
    @Override
    public void addUser(User user) {
        String passwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(passwd);
        userMapper.insert(user);
    }


}
